package es.dk.hl.view.helper
{
	import com.roguedevelopment.objecthandles.ObjectHandles;
	
	import es.dk.hl.event.FBEvent;
	import es.dk.hl.model.HomeLinkModelLocator;
	import es.dk.hl.view.ChoosePhotos;
	import es.dk.hl.view.Desktop;
	import es.dk.hl.view.Wall;
	import es.dk.hl.vo.PhotoVO;
	
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.menuClasses.MenuBarItem;
	import mx.core.UIComponent;
	import mx.managers.PopUpManager;

	public class DesktopHelper
	{
		
		public var choosePhotos:ChoosePhotos;		
		
		public var view : Desktop;
		
		public function DesktopHelper()
		{
		}
		
		public function creationCompleteHandler() : void
		{
			view.setCurrentState(view.state_started);
			//view.setCurrentState(view.state_logged);
			
			var homeLinkModelLocator:HomeLinkModelLocator = HomeLinkModelLocator.getInstance();
			BindingUtils.bindSetter(
				function ():void {
					if (HomeLinkModelLocator.getInstance().nameUser != "")
						view.setCurrentState(view.state_logged);
				},
				homeLinkModelLocator,"nameUser");
				
			/*
			var bindtip:Function = 
				function (object:Object):void{
					switch (HomeLinkModelLocator.getInstance().statusFaceBookConnect) {
						case HomeLinkModelLocator.statusFaceBookConnect_LOGIN:
							view.fButton.label = "Login";
							view.fButton.toolTip = "Fill FaceBook form to Login";		
						break;
						case HomeLinkModelLocator.statusFaceBookConnect_CONNECT:
							view.fButton.label = "Validate";
							view.fButton.toolTip = "Click to Validate FaceBook";											
						break;
						case HomeLinkModelLocator.statusFaceBookConnect_CONNECTED:
							view.fButton.label = "Logout";
							view.fButton.toolTip = "Click to Logout FaceBook";		
						break;
						case HomeLinkModelLocator.statusFaceBookConnect_LOGOUT:
							view.fButton.label = "Login";		
						break;												
					};
				}		
			var homeLinkModelLocator:HomeLinkModelLocator = HomeLinkModelLocator.getInstance();
			BindingUtils.bindSetter(bindtip,homeLinkModelLocator,"statusFaceBookConnect");
			*/
		}
		
		public function menuBarRoom_clickHandler(event:MouseEvent):void
		{
			if (event.target is MenuBarItem){
				choosePhotos = ChoosePhotos(PopUpManager.createPopUp(view,ChoosePhotos,true))
				choosePhotos.helper.photo = new PhotoVO();
				if (UIComponent(MenuBarItem(event.target).data).id == "iButton"){
					choosePhotos.helper.photo.photoType = PhotoVO.item;
				} else if (UIComponent(MenuBarItem(event.target).data).id == "wButton"){
					choosePhotos.helper.photo.photoType = PhotoVO.wall				
				}
									
				choosePhotos.helper.viewParent = Wall(view.viewStack.selectedChild);
				PopUpManager.centerPopUp(choosePhotos);
			}
		}
		public var editing:Boolean = false;
		public function menuBarArchive_clickHandler(event:MouseEvent):void
		{
			if (event.target is MenuBarItem){
				
				if (UIComponent(MenuBarItem(event.target).data).id == "eButton"){
					var array:Array = view.wall0.getChildren();
					function callbackEditing(element:*, index:int, arr:Array):void {
						if (element is ObjectHandles){
							ObjectHandles(element).mouseChildren = true;
							ObjectHandles(element).allowVMove = false;
							ObjectHandles(element).allowHMove = false;
							ObjectHandles(element).allowVResize = false;
							ObjectHandles(element).allowHResize = false;
							ObjectHandles(element).allowRotate = false;
						}
					}
					function callbackNotEditing(element:*, index:int, arr:Array):void {
						if (element is ObjectHandles){
							ObjectHandles(element).mouseChildren = false;
							ObjectHandles(element).allowVMove = true;
							ObjectHandles(element).allowHMove = true;
							ObjectHandles(element).allowVResize = true;
							ObjectHandles(element).allowHResize = true;
							ObjectHandles(element).allowRotate = true;
						}
					}					
					if (editing){
						editing = false;
						array.forEach(callbackNotEditing);
					} else {
						editing = true;
						array.forEach(callbackEditing);
					}
					
				} else if (UIComponent(MenuBarItem(event.target).data).id == "sButton"){
				}
				
			}
		}
		
		public function menuBarConnect_clickHandler(event:MouseEvent):void
		{
			if (event.target is UIComponent){
		
				if (UIComponent(event.target).id == "fButton"){
					if (view.currentState == view.state_started){
						new FBEvent(FBEvent.FBEvent_VALIDATELOGIN_LOGIN).dispatch();
					}
					if (view.currentState == view.state_logged){
						HomeLinkModelLocator.getInstance().composicion = new Date().toDateString();
						new FBEvent(FBEvent.FBEvent_SAVE).dispatch();
					}
				}
				
			}
		}
	}
}